import {cleanToken, getToken, setToken} from '@/util/cookie'
import {getUserInfo, userLogin, userLogout} from '@/api/'
import routers from "@/router"
import router from "@/router/router"

export default {
    state: {
        token: getToken(),
        hasGetInfo: false,
        avatar: '',
        lastLoginTime: null,
        nickName: '',
        ip: '',
        email: '',
        roles: [],
        perms: []
    },
    mutations: {
        setRoles(state, data) {
            state.roles = data
        },
        setPerms(state, data) {
            state.perms = data
        },
        setAvatar(state, data) {
            state.avatar = data
        },
        setLastLoginTime(state, data) {
            state.lastLoginTime = data
        },
        setNickName(state, data) {
            state.nickName = data
        },
        setIp(state, data) {
            state.ip = data
        },
        setEmail(state, data) {
            state.email = data
        },
        setToken(state, token) {
            state.token = token
            setToken(token)
        },
        setHasGetInfo(state, data) {
            state.hasGetInfo = data
        },
        logout(state) {
            state.token = ''
            cleanToken()

        }
    },
    getters: {},
    actions: {
        getUserInfo({commit}) {
            return new Promise((resolve, reject) => {
                try {
                    getUserInfo().then(res => {
                        res = res.data || {}
                        const data = res.user || {}
                        commit('setAvatar', data.avatar)
                        commit('setNickName', data.nickName)
                        commit('setIp', data.ip)
                        commit('setEmail', data.email)
                        commit('setLastLoginTime', data.lastLoginTime)
                        commit('setRoles', data.roleList)
                        commit('setPerms', data.permList)
                        commit('setHasGetInfo', true)
                        resolve(data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        },
        login({commit}, loginForm) {
            return new Promise((resolve, reject) => {
                userLogin(loginForm).then((response) => {
                    commit('setToken', response.data.token)
                    resolve(response)
                }).catch(e => {
                    reject(e)
                })
            })
        },
        logout({commit}) {
            return new Promise((resolve, reject) => {
                routers.options.routes = router
                routers.options.isAddDynamicMenuRoutes = false
                routers.addRoutes(router);
                userLogout().then((response) => {
                    commit('logout')
                    resolve(response)
                }).catch(e => {
                    reject(e)
                })
            })
        }
    }
}
